package hotel.app;

import org.apache.log4j.PropertyConfigurator;

import hotel.common.DaoConfig;
import hotel.dao.HotelDao;
import hotel.modal.User;
import hotel.controller.LoginController;
import hotel.controller.MainSceneController;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;


public class Main extends Application {
	User user = new User();
	@Override
	public void start(Stage stage) {
		try {
			Boolean debug = false; 
			debug = false;	// 调试模式
			FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/hotel/view/MainScene.fxml"));
			Parent root = (Parent)fxmlLoader.load();
			MainSceneController controller = fxmlLoader.<MainSceneController>getController();
			Scene scene = new Scene(root);
			stage.setScene(scene);
			stage.setTitle("小型客房管理系统");
			stage.setScene(scene);
			
			if(!debug) {
			FXMLLoader tfxmlLoader = new FXMLLoader(getClass().getResource("/hotel/view/Login.fxml"));
			Parent tRoot = (Parent)tfxmlLoader.load();
			LoginController tcontroller = tfxmlLoader.<LoginController>getController();
			tcontroller.setInitData(user);
			Stage tStage = new Stage();
			tStage.setTitle("小型客房管理系统登录");
			tStage.setScene(new Scene(tRoot));
			tStage.showAndWait();
			tStage.close();
			}else {
				user = HotelDao.getUser("root");
			}
			//scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
			//Parent root = (Parent)FXMLLoader.load(getClass().getResource("MainScene.fxml"));
			//Parent root = (Parent)FXMLLoader.load(getClass().getResource("UserManage.fxml"));
			//Parent root = (Parent)FXMLLoader.load(getClass().getResource("EmployeeManage.fxml"));
			if(debug) test();
			if(debug || user.isModified()) { // 如果用户验证成功加载主页面
				controller.setInitData(user);
				stage.show();
			}else {
				stage.close();
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void stop(){
	    //System.out.println("Stage is closing");
	    // Save file
	}
	
	public static void main(String[] args) {
		PropertyConfigurator.configure("log4j.properties");		// 加载日志记录配置 
		DaoConfig.load();										// 加载数据库配置
		launch(args);
	}
	
	public void test() {
		//Logger log = Logger.getLogger(Main.class);
		//Dao dao = DaoUp.me().dao();
		//log.debug(HotelDao.countRealCharge("2017-08-20 00:00:00", "2017-08-30 23:59:59"));
	}
}
